// 2013-9-1下午01:08:57  net.betou.trade.main.manager.impl.ContentMngImpl.java

package net.betou.trade.main.manager.impl;

import java.util.List;

import net.betou.common.hibernate3.Updater;
import net.betou.common.page.Pagination;
import net.betou.core.entity.ConfigLogin;
import net.betou.core.service.PostDataListener;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.Content;
import net.betou.trade.entity.main.User;
import net.betou.trade.main.dao.ContentDao;
import net.betou.trade.main.manager.ChannelMng;
import net.betou.trade.main.manager.ContentMng;
import net.betou.trade.main.manager.UserMng;
import net.betou.trade.role.setting.CommonFinderConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * @author 叶装装
 */
@Service
@Transactional
public class ContentMngImpl implements ContentMng{

	public Content deleteById( Integer id ) {
		return dao.deleteById( id );
	}

	public Content [ ] deleteByIds( Integer [ ] ids ) {
		Content [ ] beans = new Content [ ids.length ];

		for ( int i = 0; i < ids.length; i++ ){
			beans[ i ] = dao.deleteById( ids[ i ] );
		}
		return beans;
	}

	@Transactional( readOnly = true )
	public Content findById( Integer id ) {
		return dao.findById( id );
	}

	@Transactional( readOnly = true )
	public Content findMyContent( Integer id, Integer adminId, boolean system ) {
		if( system ){
			return dao.findById( id );
		}

		return dao.findMyContent( id, adminId );
	}

	@Transactional( readOnly = true )
	public Pagination getPage( AgentAndAdmins admin, String searchProperty, String searchValue, String orderProperty, String orderDirection, int pageNo, int pageSize ) {
		boolean system = admin.getSystem( );

		Pagination pagination;

		if( system ){
			pagination = dao.getPage( null, searchProperty, searchValue, orderProperty, orderDirection, pageNo, pageSize );
		}else{

			CommonFinderConfig config = new CommonFinderConfig( admin, ConfigLogin.CONFIG_CONTENT );

			if( config.isSelf( ) ){
				pagination = dao.getPage( admin.getId( ), searchProperty, searchValue, orderProperty, orderDirection, pageNo, pageSize );
			}else{
				pagination = dao.getPageByCriteria( searchProperty, searchValue, orderProperty, orderDirection, pageNo, pageSize, config.getDeepSQL( ) );
			}
		}

		return pagination;

	}

	public Content save( Content bean, AgentAndAdmins admin, Integer channelId ) {
		bean.init( );
		bean.setAdmin( admin );
		bean.setChannel( channelMng.findById( channelId ) );
		
		afterSave( bean );
		
		return dao.save( bean );
	}

	public Content update( Content bean, Integer channelId ) {

		bean.initUpdate( );
		bean.setChannel( channelMng.findById( channelId ) );

		Updater< Content > updater = new Updater< Content >( bean, Updater.UpdateMode.MIN );

		updater.include( "title" ); // 使用最小模式，只更新名称，其余全都不更新
		updater.include( "content" );
		updater.include( "recommend" );
		updater.include( "channel" );
		updater.include( "description" );
		updater.include( "send" );

		bean = dao.updateByUpdater( updater );
		
		afterUpdate( bean );
		
		return bean;
	}

	public Content update( Content bean, boolean created ) {

		bean.setCreated( created );

		Updater< Content > updater = new Updater< Content >( bean, Updater.UpdateMode.MIN );

		updater.include( "created" );

		return dao.updateByUpdater( updater );
	}
	
	private void afterUpdate( Content bean ){
		for( PostDataListener< Content > p : listenerList ){
			p.afterChange( bean, null );
		}
	}
	
	private void afterSave( Content bean ){
		for( PostDataListener< Content > p : listenerList ){
			p.afterSave( bean );
		}
	}

	private List< PostDataListener< Content > > listenerList;

	public void setListenerList( List< PostDataListener< Content >> listenerList ) {
		this.listenerList = listenerList;
	}
	
	@Transactional( readOnly = true )
	public Pagination getPage(  User user, int pageNo, int pageSize ){
		List< Integer > idList = userMng.getSearchId( user );
		if( idList.isEmpty( ) ){
			idList.add( user.getAdmin( ).getId( ) );
		}
		
		Integer groupId = user.getGroup( ).getId( );
		
		List< Integer > channelIdList = channelMng.getOrientedGroup(groupId , idList );
		if( channelIdList.isEmpty( ) ){
			return new Pagination( );
		}
		return dao.getPage( groupId, channelIdList, pageNo, pageSize );
	}

	@Autowired
	private ContentDao dao;

	@Autowired
	private ChannelMng channelMng;
	
	@Autowired
	private UserMng userMng;

}
